//
//  HeaderView.m
//  MomentKit
//
//  Created by LEA on 2017/12/14.
//  Copyright © 2017年 LEA. All rights reserved.
//

#import "HeaderView.h"
#import "SheetSetView.h"

// 图片间距
#define kImagePadding       5
// 图片宽度
#define kImageWidth         (self.bounds.size.width-15)/3.0

#pragma mark - ------------------ 小图List显示视图 ------------------

@interface HeaderView ()


@property(nonatomic, assign)Boolean  pageBlock;
@property(nonatomic, assign)long  ate;
@property(nonatomic, assign)double  arrOctober;

// 图片视图数组
@property (nonatomic, strong) NSMutableArray *imageViewsArray;
// 预览视图
@property (nonatomic, strong) SheetSetView *previewView;

@end

@implementation HeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 小图(九宫格)
        _imageViewsArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < 9; i++) {
            ButtonView *imageView = [[ButtonView alloc] initWithFrame:CGRectZero];
            imageView.tag = 1000 + i;
            [imageView setTapSmallView:^(ButtonView *imageView){
                [self phoneBtn:imageView];
            }];
            [_imageViewsArray addObject:imageView];
            [self addSubview:imageView];
        }
        // 预览视图
        _previewView = [[SheetSetView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    }
    return self;
}

#pragma mark - Setter
- (void)setModel:(HTModel *)model
{
    _model = model;
    for (ButtonView *imageView in _imageViewsArray) {
        imageView.hidden = YES;
    }
    // 图片区
    NSInteger count = model.imagePhotos.count;
    if (count == 0) {
        self.size = CGSizeZero;
        return;
    }
    // 更新视图数据
    _previewView.pageNum = count;
    _previewView.scrollView.contentSize = CGSizeMake(_previewView.width*count, _previewView.height);
    // 添加图片
    ButtonView *imageView = nil;
    for (NSInteger i = 0; i < count; i++)
    {
        if (i > 8) {
            break;
        }
        NSInteger rowNum = i/3;
        NSInteger colNum = i%3;
        if(count == 4) {
            rowNum = i/2;
            colNum = i%2;
        }
        
        CGFloat imageX = colNum * (kImageWidth + kImagePadding);
        CGFloat imageY = rowNum * (kImageWidth + kImagePadding);
        CGRect frame = CGRectMake(imageX, imageY, kImageWidth, kImageWidth);
        
        //单张图片需计算实际显示size
//        if (count == 1) {
//            CGSize singleSize = [HTSingleton getSingleSize:CGSizeMake(kImageWidth, kImageWidth)];
//            frame = CGRectMake(0, 0, singleSize.width, singleSize.height);
//        }
        imageView = [self viewWithTag:1000+i];
        imageView.hidden = NO;
        imageView.frame = frame;
        UIImage *image = [UIImage imageWithData:model.imagePhotos[i]];
        imageView.image = image;
    }
//    self.width = kTextWidth;
    self.height = imageView.bottom;
}

#pragma mark - 小图单击
- (void)phoneBtn:(ButtonView *)imageView
{
    UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    // 解除隐藏
    [window addSubview:_previewView];
    [window bringSubviewToFront:_previewView];
    // 清空
    [_previewView.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    // 添加子视图
    NSInteger index = imageView.tag-1000;
    NSInteger count = _model.imagePhotos.count;
    CGRect convertRect;
    if (count == 1) {
        [_previewView.pageControl removeFromSuperview];
    }
    for (NSInteger i = 0; i < count; i ++)
    {
        // 转换Frame
        ButtonView *pImageView = (ButtonView *)[self viewWithTag:1000+i];
        convertRect = [[pImageView superview] convertRect:pImageView.frame toView:window];
        // 添加
        TransitionView *scrollView = [[TransitionView alloc] initWithFrame:CGRectMake(i*_previewView.width, 0, _previewView.width, _previewView.height)];
        scrollView.tag = 100+i;
        scrollView.maximumZoomScale = 2.0;
        scrollView.image = pImageView.image;
        scrollView.contentRect = convertRect;
        // 单击
        [scrollView setTapBigView:^(TransitionView *scrollView){
            [self containerView:scrollView];
        }];
        // 长按
        [scrollView setLongPressBigView:^(TransitionView *scrollView){
            [self comapreDate:scrollView];
        }];
        [_previewView.scrollView addSubview:scrollView];
        if (i == index) {
            [UIView animateWithDuration:0.3 animations:^{
                _previewView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
                _previewView.pageControl.hidden = NO;
                [scrollView cellHeightWithModel];
            }];
        } else {
            [scrollView cellHeightWithModel];
        }
    }
    // 更新offset
    CGPoint offset = _previewView.scrollView.contentOffset;
    offset.x = index * ScreenWidth;
    _previewView.scrollView.contentOffset = offset;
}

#pragma mark - 大图单击||长按
- (void)containerView:(TransitionView *)scrollView
{
    [UIView animateWithDuration:0.3 animations:^{
        _previewView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        _previewView.pageControl.hidden = YES;
        scrollView.contentRect = scrollView.contentRect;
        scrollView.zoomScale = 1.0;
    } completion:^(BOOL finished) {
        [_previewView removeFromSuperview];
    }];
}

- (void)comapreDate:(TransitionView *)scrollView
{
    
}

@end

#pragma mark - ------------------ 单个小图显示视图 ------------------
@implementation ButtonView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.clipsToBounds  = YES;
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.contentScaleFactor = [[UIScreen mainScreen] scale];
        self.userInteractionEnabled = YES;
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(defaultNumSet:)];
//        [self addGestureRecognizer:tap];
    }
    return self;
}

-(Boolean)logMonthly:(CGFloat)modity eftSeptember:(NSArray *)eftSeptember launch:(NSArray *)launch {
    BOOL greetDestruction = NO;

    return greetDestruction;

}





- (void)defaultNumSet:(UIGestureRecognizer *)gesture
{

         Boolean otter = [self logMonthly:2830.0 eftSeptember:@[@"clown", @"jeans", @"spoon"] launch:[NSArray arrayWithObjects:@(389), @(420), @(768), nil]];

      if (!otter) {
          NSLog(@"logImage");
      }



    if (self.tapSmallView) {
        self.tapSmallView(self);
    }
}

@end
